import os
import pandas as pd

# 读取xlsx文件
data = pd.read_excel(r'D:\GJ\项目\铜锣山\data\wanji_event.xlsx')

# 将updateTime列转换为datetime类型
data['updateTime'] = pd.to_datetime(data['updateTime'])

# 提取日期部分
data['date'] = data['updateTime'].dt.date

# 按日期分组
groups = data.groupby('date')

data['A'] = data['orgCode'] + ',' + data['upOrgCode']


def process_group(group):
    # 对每组数据进行处理
    grouped_by_A = group.groupby('A')
    return grouped_by_A

# 对每组数据进行处理
processed_data = groups.apply(process_group)

import matplotlib.pyplot as plt


def plot_data(group):
    # 获取时间序列
    times = group['updateTime'].dt.time
    # 获取secLevel值
    sec_levels = group['secLevel']
    print(times)
    print(sec_levels)

    # 创建图表
    plt.figure(figsize=(10, 6))
    plt.scatter(times, sec_levels, color='blue', label='secLevel')
    plt.xlabel('Time of Day')
    plt.ylabel('secLevel')
    plt.title('secLevel Distribution Over Time')
    plt.xticks(pd.date_range(start='00:00:00', end='23:59:59', freq='H').time)
    plt.legend()
    plt.show()
    # # 保存图表到文件
    # output_dir = os.path.join(os.path.dirname(self.path), 'png')
    # if not os.path.exists(output_dir):
    #     os.makedirs(output_dir)
    # file_name = os.path.basename(self.path).split('.')[0]
    # output_filename = os.path.join(output_dir, file_name + '.png')
    # plt.savefig(output_filename, dpi=200)
    # # 关闭图表以释放内存
    # plt.close()


# 对每组数据进行绘图
for date, group in processed_data.items():
    for A, subgroup in group:
        plot_data(subgroup)
